<script lang="ts" setup name="Goods">
import useStore from '@/store'
import { storeToRefs } from 'pinia'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import { onMounted, ref, watchEffect } from 'vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message'
import { CartItem } from '@/types/cart'
const { goods, cart } = useStore()
const { goodsInfo } = storeToRefs(goods)
const route = useRoute()
watchEffect(() => {
  if (route.path === `/goods/${route.params.id}` && route.params.id) {
    // 清空上一次的商品信息
    goods.resetGoodsInfo()
    // 再获取新的商品信息
    goods.getGoodsInfo(route.params.id as string)
  }
})
const addSkuId = ref('')
const checkedGoods = (skuId: string) => {
  console.log(skuId) // 拿到skuId更新视图  价格 库存
  addSkuId.value = skuId
  const sku = goodsInfo.value.skus.find((sku) => sku.id === skuId)
  if (sku) {
    // 更新数据这块 严格来说应该是要做一个action的
    goodsInfo.value.inventory = sku.inventory
    goodsInfo.value.oldPrice = sku.oldPrice
    goodsInfo.value.price = sku.price
  }
}
// 计数器组件的数据
const num = ref(1)
// 加入购物车
const addCart = async () => {
  // console.log(addSkuId.value)
  // 保证用户选择了完整的规格 如果没有选中一个确切的规格, 提示用户
  if (!addSkuId.value) return Message.warning('请选择完整的商品规格')
  console.log(addSkuId.value)
  // 处理本地购物车的规格字段
  // 1. 从goodsInfo的所有的skus数组 筛选出和addSkuId.value一样的sku
  const sku = goodsInfo.value.skus.find((item) => item.id === addSkuId.value)
  // console.log(sku)
  // 2. 找到的sku有specs 遍历 拼接规格信息
  const attrs = sku?.specs.map((item) => `${item.name}: ${item.valueName}`).join(',')
  // console.log(attrs)
  await cart.addCart({
    id: goodsInfo.value.id, // 购买商品的id
    name: goodsInfo.value.name, // 购买商品的名字
    picture: goodsInfo.value.mainPictures[0], //商品展示的图片
    price: goodsInfo.value.price, // 商品的现价格
    count: num.value, // 商品购买的数量
    skuId: addSkuId.value, // 购买商品的skuId
    attrsText: attrs, // 购买商品的规格
    selected: true, // 是否选中
    nowPrice: goodsInfo.value.price, // 商品的老价格
    stock: goodsInfo.value.inventory, // 商品的库存
    isEffective: true, // 是否有效
  } as CartItem)
  Message.success('加入购物车成功')
}
</script>
<template>
  <div class="xtx-goods-page" v-if="goodsInfo.id">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <template v-if="goodsInfo.id">
          <XtxBreadItem to="/">{{ goodsInfo.categories[1].name }}</XtxBreadItem>
          <XtxBreadItem to="/">{{ goodsInfo.categories[0].name }}</XtxBreadItem>
          <XtxBreadItem>{{ goodsInfo.name }}</XtxBreadItem>
        </template>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左侧盒子 -->
        <div class="media">
          <!-- 放大镜组件 -->
          <GoodsImage :images="goodsInfo.mainPictures"></GoodsImage>
          <!-- 销量/评价/收藏组件 -->
          <GoodsSales :goodsInfo="goodsInfo"></GoodsSales>
        </div>
        <!-- 右侧盒子 -->
        <div class="spec">
          <!-- 商品介绍 + 城市选择 -->
          <GoodsName :goodsInfo="goodsInfo"></GoodsName>
          <!-- sku组件 -->
          <GoodsSku
            ref="GoodsSkuRef"
            :goodsInfo="goodsInfo"
            @checkedGoods="checkedGoods"
            :skuId="goods.filterSkus[0].id"
          ></GoodsSku>
          <!-- 计数器组件 -->
          <XtxNumbox
            v-model="num"
            :min="1"
            :max="goodsInfo.inventory"
            label="数量"
          ></XtxNumbox>
          <!-- 加入购物车按钮 -->
          <XtxButton
            @click="addCart"
            style="margin-top: 20px"
            size="middle"
            type="primary"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail :goods="goodsInfo"></GoodsDetail>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="3"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-info {
  min-height: 600px;
  background: #fff;
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
